import db from '../db/index.js'
export function getlist(req, res) {
    const { pagesize, pagenum } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select stuid,student.name,grade,IDcard,phone,nationality,submajor.name as major_name,class.cname as class_cname from student inner join submajor on student.subid = submajor.submajorid inner join class on student.classid = class.classid  limit ${pagesize} offset ${page}`
    db.query(sql, (err, data) => {
        if (err) return res.dbno(err)
        const studata = data    
        const sql2 = 'SELECT COUNT(*) as total FROM student'
        db.query(sql2, (err, data) => {
            if (err) return res.dbno(err)
                console.log(studata);
                
            res.send({ status: 200, data: studata, total: data[0].total })
        })

    })
}
export function seachstu(req, res) {
    const { condition, val, pagesize, pagenum } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select stuid,student.name,grade,IDcard,phone,nationality,submajor.name as major_name,class.cname as class_cname from student inner join submajor on student.subid = submajor.submajorid inner join class on student.classid = class.classid where ${condition}=? limit ${pagesize} offset ${page}`
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        const studata = data
        const sql2 = `select count(*) as total from student inner join submajor on student.subid = submajor.submajorid inner join class on student.classid = class.classid where ${condition}=?  `
        db.query(sql2, val, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, data: studata, total: data[0].total })
        })
    })
}
export function specialstu(req, res) {
    const sql = `select student.stuid,student.name,grade,IDcard,phone,nationality,submajor.name as major_name,class.cname as class_cname,stuspecial.reason as reason,stuspecial.start_time as start_time,stuspecial.end_time as end_time from 
    student inner join submajor on student.subid = submajor.submajorid 
    inner join class on student.classid = class.classid
    inner join stuspecial on student.stuid = stuspecial.stuid
    `
    db.query(sql, (err, data) => {
        if (err) return res.dbno(err)
        res.send({ data, status: 200 })
    })
}
export function seachspecialstu(req, res) {
    const { condition, val } = req.body
    const sql = `select student.stuid,student.name,grade,IDcard,phone,nationality,submajor.name as major_name,class.cname as class_cname,stuspecial.reason as reason,stuspecial.start_time as start_time,stuspecial.end_time as end_time from 
    student inner join submajor on student.subid = submajor.submajorid 
    inner join class on student.classid = class.classid
    inner join stuspecial on student.stuid = stuspecial.stuid where ${condition}=? `
    db.query(sql, val, (err, data) => {
        if (err) return res.dbno(err)
        res.send({ status: 200, data })
    })
}
export function honor(req, res) {
    const { stuid } = req.query
    const sql = `select student.stuid,student.name,grade,IDcard,phone,nationality,submajor.name as major_name,class.cname as class_cname from 
    student inner join submajor on student.subid = submajor.submajorid 
    inner join class on student.classid = class.classid where student.stuid =?
    `
    db.query(sql, stuid, (err, data) => {
        if (err) return res.dbno(err)
            console.log(data);
        const stuinfo = data.length>0?data[0]:[]
        const sql2 = 'select honor,create_time from honor where stuid = ?'
        db.query(sql2, stuid, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ data:{stuinfo,honorlist:data}, status: 200 })
        })

    })
}